package lg.minecraft.plugin.library.input;

import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author langle__
 * @version 1.0
 */
public class PlayerInputManager {

    private static final Map<UUID, PlayerInputHandler> PLAYER_INPUT_HANDLER = new HashMap<>();

    public static void waitForInput(Player player, PlayerInputHandler handler) {
        if (!(handler instanceof CommandInput || handler instanceof ChatInput)) {
            throw new IllegalArgumentException("PlayerInputHandler must be CommandInput or ChatInput");
        }
        PLAYER_INPUT_HANDLER.put(player.getUniqueId(), handler);
    }

    /**
     * 判断指定玩家是否正在等待特定类型的输入
     *
     * @param player 玩家对象
     * @param type   输入类型
     * @return 是否匹配指定的输入类型
     */
    public static boolean isWaitingForInput(Player player, InputType type) {
        PlayerInputHandler handler = PLAYER_INPUT_HANDLER.get(player.getUniqueId());
        if (handler == null) return false;

        if (type == InputType.CHAT) {
            return handler instanceof ChatInput;
        } else if (type == InputType.COMMAND) {
            return handler instanceof CommandInput;
        }

        return false;
    }

    public static void handleInput(Player player, String input) {
        UUID uuid = player.getUniqueId();
        PlayerInputHandler handler = PLAYER_INPUT_HANDLER.remove(uuid);
        if (handler != null) {
            handler.onPlayerInput(player, input);
        }
    }

    public static List<String> handleInput(Player player, String[] args) {
        UUID uuid = player.getUniqueId();
        PlayerInputHandler handler = PLAYER_INPUT_HANDLER.get(uuid);
        if (handler != null) {
            if (handler instanceof CommandInput) {
                return ((CommandInput) handler).onTabComplete(player, args);
            }
        }
        return null;
    }

}
